﻿using AutoMapper;
using FluentValidation;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    [ApiController]
    [Route("/api/v1")]
    public class MaterialClassTypeController : Controller
    {
        private readonly ILogger<MaterialClassTypeController> _logger;
        private readonly IMaterialClassTypeService _materialClassTypeService;
        private readonly IMapper _mapper;
        private readonly IValidator<MaterialClassTypeInput> _materialClassTypeInputValidator;
        private readonly IValidator<MaterialClassTypeUpdateInput> _materialClassTypeUpdateInputValidator;
        private readonly IValidator<MaterialClassTypePageInput> _materialClassTypePageInputValidator;

        public MaterialClassTypeController(ILogger<MaterialClassTypeController> logger,
            IMaterialClassTypeService materialClassTypeService,
            IMapper mapper,
            IValidator<MaterialClassTypeInput> materialClassTypeInputValidator,
            IValidator<MaterialClassTypeUpdateInput> materialClassTypeUpdateInputValidator,
            IValidator<MaterialClassTypePageInput> materialClassTypePageInputValidator)
        {
            _logger = logger;
            _materialClassTypeService = materialClassTypeService;
            _mapper = mapper;
            _materialClassTypeInputValidator = materialClassTypeInputValidator;
            _materialClassTypeUpdateInputValidator = materialClassTypeUpdateInputValidator;
            _materialClassTypePageInputValidator = materialClassTypePageInputValidator;
        }

        /// <summary>
        /// 添加物料类类型
        /// </summary>
        /// <param name="materialClassTypeInput"></param>
        /// <returns></returns>
        [HttpPost("materialclasstypes")]
        public async Task<ActionResult> AddMaterialClassType(MaterialClassTypeInput materialClassTypeInput)
        {
            try
            {
                var input = await _materialClassTypeInputValidator.ValidateAsync(materialClassTypeInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _materialClassTypeService.AddMaterialClassTypeAsync(materialClassTypeInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 修改物料类类型
        /// </summary>
        /// <param name="materialClassTypeUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("materialclasstypes")]
        public async Task<ActionResult> UpdateMaterialClassType(MaterialClassTypeUpdateInput materialClassTypeUpdateInput)
        {
            try
            {
                var input = await _materialClassTypeUpdateInputValidator.ValidateAsync(materialClassTypeUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _materialClassTypeService.UpdateMaterialClassTypeAsync(materialClassTypeUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除物料类类型
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("materialclasstypes")]
        public async Task<ActionResult> DeleteMaterialClassTypes(int[] ids)
        {
            try
            {
                await _materialClassTypeService.DeleteMaterialClassTypeAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询物料类类型
        /// </summary>
        /// <param name="materialClassTypePageInput"></param>
        /// <returns></returns>
        [HttpGet("materialclasstypes")]
        public async Task<ActionResult> GetMaterialClassType([FromQuery] MaterialClassTypePageInput materialClassTypePageInput)
        {
            try
            {
                var input = await _materialClassTypePageInputValidator.ValidateAsync(materialClassTypePageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _materialClassTypeService.GetMaterialClassTypesAsync(materialClassTypePageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 根据主键编号查询物料类类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("materialclasstypes/entity")]
        public async Task<ActionResult> GetMaterialClassTypeEntityById(int id)
        {
            try
            {
                var result = await _materialClassTypeService.GetMaterialClassTypeAsync(id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
